package com.holly.unit.bpmn.designer.core.adapter;

import com.alibaba.fastjson.JSON;
import com.holly.unit.bpmn.designer.core.bpmn.BpmnModelFactory;
import com.holly.unit.bpmn.designer.model.BpmnDesignerModel;
import lombok.extern.slf4j.Slf4j;
import org.activiti.bpmn.BpmnAutoLayout;
import org.activiti.bpmn.converter.BpmnXMLConverter;
import org.activiti.bpmn.model.Process;
import org.activiti.bpmn.model.*;

import java.util.List;
/**
 * 类描述: BpmnDesignerXml 负责创建BPMN元素
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/5/12 10:16
 */
@Slf4j
public class BpmnDesignerXml {

    private BpmnModelFactory factory;

    public BpmnDesignerXml(BpmnModelFactory factory) {
        this.factory = factory;
    }

    public BpmnBuilderSection builder() {
        return new BpmnBuilderSection();
    }


    public class BpmnBuilderSection {

        public ModelSection model(BpmnDesignerModel model) {
            return new ModelSection(model);
        }

        public class ModelSection {
            private BpmnDesignerModel model;

            public ModelSection(BpmnDesignerModel model) {
                this.model = model;
            }

            public UserTaskSection task(List<UserTask> userTaskList) {
                return new UserTaskSection(userTaskList);
            }

            public class UserTaskSection {
                private List<UserTask> userTaskList;

                public UserTaskSection(List<UserTask> userTaskList) {
                    this.userTaskList = userTaskList;
                }

                public LineSection line(List<SequenceFlow> sequenceFlowList) {
                    return new LineSection(sequenceFlowList);
                }

                public class LineSection {
                    private List<SequenceFlow> sequenceFlowList;

                    public LineSection(List<SequenceFlow> sequenceFlowList) {
                        this.sequenceFlowList = sequenceFlowList;
                    }

                    public GateWaySection gateway(List<ExclusiveGateway> gatewayList) {
                        return new GateWaySection(gatewayList);
                    }

                    public class GateWaySection {
                        private List<ExclusiveGateway> gatewayList;

                        public GateWaySection(List<ExclusiveGateway> gatewayList) {
                            this.gatewayList = gatewayList;
                        }

                        public String  generate() throws Exception{
                            System.out.println(JSON.toJSONString(model,true));
                            BpmnModel bpmnModel = new BpmnModel();


                            Process process = new Process();
                            bpmnModel.addProcess(process);

                            process.setId(model.getBasicInfo().getFlowCode());
                            process.setName(model.getBasicInfo().getFlowName());

                            // 创建开始节点
                            StartEvent startEvent = factory.createStartEvent(model.getProcessInfo().getNodeId(), model.getBasicInfo().getFlowName());

                            // 创建结束节点
                            EndEvent endEvent = factory.createEndEvent();

                            process.addFlowElement(startEvent);
                            process.addFlowElement(endEvent);

                            // 设置任务节点
                            for (UserTask userTask : userTaskList) {
                                process.addFlowElement(userTask);
                            }

                            // 设置网关
                            for (ExclusiveGateway gateway : gatewayList) {
                                process.addFlowElement(gateway);
                            }

                            // 设置连线
                            for (SequenceFlow sequenceFlow : sequenceFlowList) {
                                process.addFlowElement(sequenceFlow);
                            }

                            // BPMN布局
                            new BpmnAutoLayout(bpmnModel).execute();

                            // 把BpmnModel转化成xml
                            BpmnXMLConverter bpmnXMLConverter = new BpmnXMLConverter();
                            byte[] coverToXml = bpmnXMLConverter.convertToXML(bpmnModel);
                            String rst = new String(coverToXml,"UTF-8");
                            rst = rst.replaceAll("&lt;", "<").replaceAll("&gt", ">");
                            return rst;
                        }
                    }

                }
            }
        }
    }
}
